package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.modules.project.entity.ProjectGroupEntity;
import io.renren.modules.project.entity.ProjectPlanEntity;
import io.renren.modules.project.service.ProjectGroupService;
import io.renren.modules.project.service.ProjectPlanService;
import io.renren.modules.project.vo.CheckOutputTempVoEntity;
import io.renren.modules.project.vo.CheckOutputVoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.project.dao.CheckOutputTempDao;
import io.renren.modules.project.entity.CheckOutputTempEntity;
import io.renren.modules.project.service.CheckOutputTempService;
import org.springframework.transaction.annotation.Transactional;


@Service("checkOutputTempService")
public class CheckOutputTempServiceImpl extends ServiceImpl<CheckOutputTempDao, CheckOutputTempEntity> implements CheckOutputTempService {

    @Autowired
    private ProjectGroupService projectGroupService;
    @Autowired
    private ProjectPlanService projectPlanService;

    @Override
    public List<CheckOutputTempEntity> queryList(Map<String, Object> params) {
        String projectNo = (String)params.get("projectNo");

        List<CheckOutputTempEntity> list = this.selectList(
                new EntityWrapper<CheckOutputTempEntity>().like(StringUtils.isNotBlank(projectNo),"project_no", projectNo)
        );

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(CheckOutputTempVoEntity checkOutputTempVoEntity){
        this.deleteByMap(new MapUtils().put("project_no" , checkOutputTempVoEntity.getprojectNo()));
        List<CheckOutputTempEntity> templist = new ArrayList<>();
        Float sum = 0f;
        for(CheckOutputVoEntity checkvo : checkOutputTempVoEntity.gettempOutputlist()){
            if(checkvo.getChecked()) {
                CheckOutputTempEntity temp = new CheckOutputTempEntity();
                temp.setProjectNo(checkOutputTempVoEntity.getprojectNo());
                temp.setTypeId(checkvo.gettypeId());
                temp.setProjectRatio(checkvo.getprojectRatio());
                temp.setWorkLoad(checkvo.getworkLoad());
                templist.add(temp);
                sum += checkvo.getUnitOutput() * checkvo.getprojectRatio() * checkvo.getworkLoad();
            }
        }
        // 修改预算产值后 修改工作组的预算产值
        if( templist.size() > 0) {
            this.insertBatch(templist);
            ProjectPlanEntity pEntity = new ProjectPlanEntity();
            pEntity.setProjectOutput(String.valueOf(sum));
            pEntity.setProjectNo(checkOutputTempVoEntity.getprojectNo());
            projectPlanService.saveTemp(pEntity);
            List<ProjectGroupEntity> gList = projectGroupService.selectList(
                    new EntityWrapper<ProjectGroupEntity>().eq("project_no",checkOutputTempVoEntity.getprojectNo())
            );
            if (gList.size() > 0) {
                for(ProjectGroupEntity groupEntity : gList) {
                    groupEntity.setProjectOutput(sum * groupEntity.getOutputRate() / 100);
                    groupEntity.setShortDateTime( (float)(Math.ceil(sum / 1200) * 0.5f));
                    groupEntity.setLastDateTime( (float)(Math.ceil(sum / 1200 * 3 / 2) * 0.5f));
                }
                projectGroupService.updateBatchById(gList);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CheckOutputTempEntity entity) {
        this.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CheckOutputTempEntity entity) {
        this.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除
        this.deleteBatchIds(Arrays.asList(roleIds));
    }

}